package com.theme.passport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.SysDocumentDao;
import com.theme.passport.entity.SysDocumentEntity;
import com.theme.passport.service.SysDocumentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("sysDocumentService")
public class SysDocumentServiceImpl extends ServiceImpl<SysDocumentDao, SysDocumentEntity> implements SysDocumentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("key");
        String typeId = (String) params.get("typeId");
        IPage<SysDocumentEntity> page = this.page(
                new Query<SysDocumentEntity>().getPage(params),
                new QueryWrapper<SysDocumentEntity>()
                        .eq(StringUtils.isNotBlank(typeId), "type_id", typeId)
                        .like(StringUtils.isNotBlank(keyword), "title", keyword)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

    @Override
    public SysDocumentEntity queryDocumentByTypeId(Map<String, Object> params) {
        String typeId = (String) params.get("typeId");
        String state = (String) params.get("state");
        IPage<SysDocumentEntity> page = this.page(
                new Query<SysDocumentEntity>().getPage(params),
                new QueryWrapper<SysDocumentEntity>()
                        .eq(StringUtils.isNotBlank(typeId), "type_id", typeId)
                        .eq(StringUtils.isNotBlank(state), "state", state)
                        .orderByDesc("create_time")
        );
        List<SysDocumentEntity> records = page.getRecords();
        if (records == null || records.size() <= 0){
            return null;
        }
        return records.get(0);
    }

}